<?php
/*
Plugin Name: RichComments!
Plugin URI: http://code.google.com/p/wp-richcomments/
Description: The Wordpress Plugin that lets your blog subscribers attach photos and videos to their comments
Author: Stefano Verna
Version: 0.1
*/

if (! class_exists( 'RichComments' )):

class RichComments {
	
	function table() {
		global $table_prefix;
		return $table_prefix . 'richcomments';
	}
	
	function getCurrentPath() {
		$url = get_settings('siteurl');	
		return $url . '/wp-content/plugins/wp-richcomments';
	}
	
	function insertRow($c, $type, $id) {
		global $wpdb;
		$q = "INSERT INTO " . RichComments::table() . ' (`comment_id`, `type`, `data`) VALUES (\''. $c . '\', \''.$type.'\', \'' . $id . '\')';
		$wpdb->query($q);
	}	
	
	function RichComments() {
		global $wpdb;
		
		add_action('admin_menu',   array('RichComments', 'admin_menu'));
		add_action('comment_post', array('RichComments', 'comment_post'));
		add_action('pingback_post', array('RichComments', 'trackback'));
		add_action('trackback_post', array('RichComments', 'trackback'));
		// Although this is theme-dependant it is fairly widely supported
		add_filter('wp_head', array('RichComments', 'head_stuff'));
		add_filter('comment_text', array('RichComments', 'getRichComment'), 9999);
	
		// init table
		$query = "CREATE TABLE IF NOT EXISTS `" . RichComments::table() . "` (`id` BIGINT(32) UNSIGNED NOT NULL auto_increment, `comment_id` BIGINT(32) UNSIGNED NOT NULL, `ts` TIMESTAMP, `type` varchar(128) NOT NULL, `data` varchar(1024) NOT NULL, PRIMARY KEY(`id`))";
		$wpdb->query($query);

		$this->trackback = false;
	}
	
	function trackback($comment_ID) {
		global $_RichComments;
		$_RichComments->trackback = true;
		return $comment_ID;
	}

	function admin_menu() {
		add_options_page('RichComments Options', 'RichComments', 8, __FILE__, array('RichComments', 'options'));	
	}
	
	function options() {
		?>
			<div class="wrap">
			<h2>RichComments! v0.1</h2>
			<p>RichComments! does not offer any options yet.</p>
			</div>
		<?php
	}

	function head_stuff() {
		global $_RichComments;
		// some imports.. thanks opensource world out there. really.
?>
	<!-- needs to be jQuery v1.2.2 or higher! -->
	<script type="text/javascript" src="<?php echo $_RichComments->getCurrentPath(); ?>/js/jquery/jquery.pack.js"></script>
	<script type="text/javascript" src="<?php echo $_RichComments->getCurrentPath(); ?>/js/l18n-js.php"></script>
	<script type="text/javascript" src="<?php echo $_RichComments->getCurrentPath(); ?>/js/richcomments.js"></script>
	<script type="text/javascript" src="<?php echo $_RichComments->getCurrentPath(); ?>/js/thickbox_reloaded/jquery.thickbox.js"></script>
<?php
	}

	function comment_post($comment_id) {
		global $user_ID, $comment_type, $_RichComments;
		require_once("services.php");
		
		$types = getServices();
		
		if ($_RichComments->trackback)
			return $comment_id;
		
		foreach ($types as $type => $data)
			if (isset($_POST[$type.'_number']))
				for ($i=0; $i<intval($_POST[$type.'_number']); $i++)
					if (isset($_POST[$type.'_'.$i])) {
						$id = getID($type, $_POST[$type.'_'.$i]);
						if ($id != "") $_RichComments->insertRow($comment_id, $type, $id);
					}
		
	}
	
	function getRichComment($comment_content) {
		require_once("services.php");
		global $comment, $_RichComments, $wpdb;
		
		$medias = $wpdb->get_results("SELECT `data`, `type` FROM " . RichComments::table() . " WHERE comment_id = '" . $comment->comment_ID . "'");
		if (count($medias) == 0) {
			return $comment_content;
		}
		
		$types = getServices();
		
		$content = '';
		foreach ($medias as $media) {
			if (isset($types[$media->type])) {
				$printer = $types[$media->type]['mediaPrinter'];
				$content .= $printer($media->data, $comment->comment_ID);
			}
		}
		
		return $comment_content . '<div id="rc_comment">'.$content.'</div>';
	}
	
}
endif;

$_RichComments = new RichComments();
?>
